package com.erb.common;

import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * Created by Administrator on 2020/4/30.
 */
@Component
public class loanFileUtils {

	/**
	 * 文件上传
	 */
	public String fileUpload(MultipartFile file, String savePath) {
		// 所以为每个文件生成一个新的文件名(asda-df43t-f34t3-23r2-34t2)
		String picName = UUID.randomUUID().toString();
		// 截取文件的扩展名(如.jpg)
		String oriName = file.getOriginalFilename();
		String extName = oriName.substring(oriName.lastIndexOf("."));
		File dirFile = new File(savePath);
		if (!dirFile.exists()) {
			dirFile.mkdirs();
		}
		//拼接新文件名
		String newFileName = picName + extName;
		File targetFile = new File(savePath, newFileName);
		try {
			file.transferTo(targetFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return newFileName;
	}
}
